<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{	
    protected function _initDb()
    {	
	
        $dbOption = $this->getOption('resources'); //lay cac tham so co dang resources.xxx.xxx trong file option(application.ini)
        
		$dbOption = $dbOption['db']; //lay cac tham so resources.db.xxx.xxx
        
		//tao 1 doi tuon Zend_db de truy cap db
		$db = Zend_Db::factory($dbOption['adapter'],$dbOption['params']); 
        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
        $db->query("SET NAMES 'utf8'");
        $db->query("SET CHARACTER SET 'utf8'");
        
		//luu connectDB vua duoc tao vao registry
		Zend_Registry::set('connectDB',$db);
		
		//thiet lap adapter mac dinh la connect vua tao
        Zend_Db_Table::setDefaultAdapter($db);
        return $db;
    }
    protected function _initViewer()
    {
        $reqSDR = 0;
        if (isset($_REQUEST['jxSystemDisR'])){
            $reqSDR = (int)$_REQUEST['jxSystemDisR'];
        }
        $reqSR = 0;
        if (isset($_REQUEST['jxSystemRequest'])){
            $reqSR = (int)$_REQUEST['jxSystemRequest'];
        }
        //echo $reqSR .'|'.$reqSDR;exit; 
        if ($reqSR == 1 && $reqSDR == 0) {
            Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true);
        }
    }
    protected function _initLayout(){
        // tach chuoi REQUEST_URI de xac dinh layout tra ve la admin hay default
		$layout = explode('/', $_SERVER['REQUEST_URI']);
		
		/* // Logging class initialization
		$writer = new Zend_Log_Writer_Stream('D:\trunk\danazen\application\log.txt');
		$logger = new Zend_Log($writer);
		$logger->info($_SERVER['REQUEST_URI']); */


        if (!in_array('default', $layout)){
            $layout_dir = 'admin';
        }else{
            $layout_dir = 'default';
        }
        $options = array(
            'layout' => 'default',
            'layoutPath' => APPLICATION_PATH . "/templates/".$layout_dir."/temp1",
        );
        $reqSR = 0;
        if (isset($_REQUEST['jxSystemRequest'])){
            $reqSR = $_REQUEST['jxSystemRequest'];
        }
        if((int)$reqSR == 0 ){
            Zend_Layout::startMvc($options);
        }
    }
}
